package UI;
import Service.SecurityService;
import Service.PriceDataServiceClient;
import Service.LatestServiceClient;
import Service.HistoryServiceClient;
import facecat.topin.core.*;
import java.util.*;
import facecat.topin.service.*;

public class DataCenter {
    /*
    * 文件分隔符号
    */
    public static String m_seperator = "/"; //Linux
    //public static String m_seperator = "\\"; //Windows
    
    /*
    * 获取程序路径
    */
    public static String getAppPath(){
        return System.getProperty("user.dir").replace("\\", "/"); //Linux
        //return System.getProperty("user.dir"); //Windows
    }
    
    public static String m_mainIP = "110.42.188.197";

    public static int m_mainPort = 9956;

    public static int m_mainKID = -1;
    
    public static SecurityService m_securityService;

    public static LatestServiceClient m_latestServiceClient;

    public static HistoryServiceClient m_historyServiceClient;

    public static PriceDataServiceClient m_priceDataServiceClient;

    public static void startService() {
        m_securityService = new SecurityService();
        m_latestServiceClient = new LatestServiceClient();
        FCClientService.addService(m_latestServiceClient);
        m_historyServiceClient = new HistoryServiceClient();
        FCClientService.addService(m_historyServiceClient);
        m_priceDataServiceClient = new PriceDataServiceClient();
        FCClientService.addService(m_priceDataServiceClient);
    }
    
     public static int connect(String ip, int port, byte[] token) {
         int socketID = FCClientService.connectToServer(0, ip, port, "", 0, "", "", "", 6, token);
         return socketID;
     }
     
    public static void startConnect()
    {
        int socketID = connect(m_mainIP, m_mainPort, new byte[] { 0, 0, 0, 0 });
        if (socketID != -1)
        {
            m_latestServiceClient.setSocketID(socketID);
            m_historyServiceClient.setSocketID(socketID);
            m_priceDataServiceClient.setSocketID(socketID);
            m_latestServiceClient.m_connected = true;
            m_historyServiceClient.reqDevideDatas(FCClientService.getRequestID());
            m_historyServiceClient.reqCodes(FCClientService.getRequestID());
            if (socketID != -1)
            {
                int tick = 1000;
                while (!m_securityService.isLoaded())
                {
                    try{
                        Thread.sleep(1);                
                    }catch(Exception ex){
                    }
                    tick--;
                    if (tick < 0)
                    {
                        break;
                    }
                }
            }
        }
    }
}

